﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UCharacterController : MonoBehaviour
{
    public GameObject Instance = null;
    public GameObject WeaponInstance = null;

    public string Skeleton;
    public string Equipment_head;
    public string Equipment_chest;
    public string Equipment_hand;
    public string Equipment_feet;

    public int Index;
    public bool Rotate = false;
    public int AnimationState = 0;
    private Animation animationController = null;

    public UCharacterController(int index, string skeleton, string weapon, string head, string chest, string hand, string feet, bool combine = false)
    {
        Object res = Resources.Load("Prefab/" + skeleton);
        Instance = Instantiate(res) as GameObject;
        Index = index;
        Skeleton = skeleton;
        Equipment_head = head;
        Equipment_chest = chest;
        Equipment_hand = hand;
        Equipment_feet = feet;

        string[] equipments = new string[4];
        equipments[0] = head;
        equipments[1] = chest;
        equipments[2] = hand;
        equipments[3] = feet;

        SkinnedMeshRenderer[] meshes = new SkinnedMeshRenderer[4];
        GameObject[] objects = new GameObject[4];
        for (int i = 0; i < equipments.Length; i++)
        {
            res = Resources.Load("Prefab/" + equipments[i]);
            objects[i] = Instantiate(res) as GameObject;
            meshes[i] = objects[i].GetComponentInChildren<SkinnedMeshRenderer>();
        }
        
        App.Game.CharacterMgr.CombineSkinnedMgr.CombineObject(Instance, meshes, combine);
        
        for (int i = 0; i < objects.Length; i++)
        {

            GameObject.DestroyImmediate(objects[i].gameObject);
        }
        
        res = Resources.Load("Prefab/" + weapon);
        WeaponInstance = GameObject.Instantiate(res) as GameObject;

        Transform[] transforms = Instance.GetComponentsInChildren<Transform>();
        foreach (Transform joint in transforms)
        {
            if (joint.name == "weapon_hand_r")
            {
                WeaponInstance.transform.parent = joint.gameObject.transform;
                break;
            }
        }
        
        WeaponInstance.transform.localScale = Vector3.one;
        WeaponInstance.transform.localPosition = Vector3.zero;
        WeaponInstance.transform.localRotation = Quaternion.identity;
        
        animationController = Instance.GetComponent<Animation>();
        PlayStand();
    }

    public void ChangeHeadEquipment(string equipment, bool combine = false)
    {
        ChangeEquipment(0, equipment, combine);
    }

    public void ChangeChestEquipment(string equipment, bool combine = false)
    {
        ChangeEquipment(1, equipment, combine);
    }

    public void ChangeHandEquipment(string equipment, bool combine = false)
    {
        ChangeEquipment(2, equipment, combine);
    }

    public void ChangeFeetEquipment(string equipment, bool combine = false)
    {
        ChangeEquipment(3, equipment, combine);
    }

    public void ChangeWeapon(string weapon)
    {
        Object res = Resources.Load("Prefab/" + weapon);
        GameObject oldWeapon = WeaponInstance;
        WeaponInstance = GameObject.Instantiate(res) as GameObject;
        WeaponInstance.transform.parent = oldWeapon.transform.parent;
        WeaponInstance.transform.localPosition = Vector3.zero;
        WeaponInstance.transform.localScale = Vector3.one;
        WeaponInstance.transform.localRotation = Quaternion.identity;

        GameObject.Destroy(oldWeapon);
    }

    public void ChangeEquipment(int index, string equipment, bool combine = false)
    {
        switch (index)
        {
            case 0:
                Equipment_head = equipment;
                break;
            case 1:
                Equipment_chest = equipment;
                break;
            case 2:
                Equipment_hand = equipment;
                break;
            case 3:
                Equipment_feet = equipment;
                break;
        }

        string[] equipments = new string[4];
        equipments[0] = Equipment_head;
        equipments[1] = Equipment_chest;
        equipments[2] = Equipment_hand;
        equipments[3] = Equipment_feet;

        Object res = null;
        SkinnedMeshRenderer[] meshes = new SkinnedMeshRenderer[4];
        GameObject[] objects = new GameObject[4];
        for (int i = 0; i < equipments.Length; i++)
        {

            res = Resources.Load("Prefab/" + equipments[i]);
            objects[i] = GameObject.Instantiate(res) as GameObject;
            meshes[i] = objects[i].GetComponentInChildren<SkinnedMeshRenderer>();
        }

        App.Game.CharacterMgr.CombineSkinnedMgr.CombineObject(Instance, meshes, combine);

        for (int i = 0; i < objects.Length; i++)
            DestroyImmediate(objects[i].gameObject);
    }

    public void PlayStand()
    {

        animationController.wrapMode = WrapMode.Loop;
        animationController.Play("breath");
        AnimationState = 0;
    }

    public void PlayAttack()
    {
        animationController.wrapMode = WrapMode.Once;
        animationController.PlayQueued("attack1");
        animationController.PlayQueued("attack2");
        animationController.PlayQueued("attack3");
        animationController.PlayQueued("attack4");
        AnimationState = 1;
    }

    // Update is called once per frame
    public void Update()
    {
        if (AnimationState == 1)
        {
            if (!animationController.isPlaying)
            {
                PlayAttack();
            }
        }
        if (Rotate)
        {
            Instance.transform.Rotate(new Vector3(0, 90 * Time.deltaTime, 0));
        }
    }
}
